package io.renren.modules.skate.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import io.renren.modules.skate.dto.FAthleteDTO;
import io.renren.modules.skate.dto.FCertificateDTO;
import io.renren.modules.skate.dto.FClassifyDTO;
import io.renren.modules.skate.dto.FClubDTO;
import io.renren.modules.skate.excel.FAthleteExcel;
import io.renren.modules.skate.excel.FGradeCertificateExcel;
import io.renren.modules.skate.service.FAthleteService;
import io.renren.modules.skate.service.FCertificateService;
import io.renren.modules.skate.service.FClassifyService;
import io.renren.modules.skate.service.FClubService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Scope("prototype")
public class FGradeCertificateListener extends AnalysisEventListener<FGradeCertificateExcel> {

    @Autowired
    private FCertificateService fCertificateService;

    @Autowired
    private FClassifyService classifyService;

    @Override
    public void invoke(FGradeCertificateExcel fGradeCertificateExcel, AnalysisContext analysisContext) {

        FCertificateDTO fCertificateDTO = new FCertificateDTO();

        if(StringUtils.isNotBlank(fGradeCertificateExcel.getClassifyName())){
            FClassifyDTO fClassifyDTO = classifyService.queryByName(fGradeCertificateExcel.getClassifyName());
            if(fClassifyDTO != null){
                fCertificateDTO.setClassifyId(fClassifyDTO.getId().intValue());
            }
        }
        fCertificateDTO.setName(fGradeCertificateExcel.getName());
        fCertificateDTO.setIdCard(fGradeCertificateExcel.getIdCard());
        fCertificateDTO.setAddress(fGradeCertificateExcel.getAddress());
        //fCertificateDTO.setScore(fGradeCertificateExcel.getScore());
        fCertificateDTO.setCertificateNo(fGradeCertificateExcel.getCertificateNo());
        fCertificateDTO.setExaminer(fGradeCertificateExcel.getExaminer());
        fCertificateDTO.setCreateDate(new Date());
        fCertificateDTO.setType(1);

        fCertificateService.save(fCertificateDTO);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
